//
//  HomeSortView.m
//  Cat
//
//  Created by lushitong on 2019/1/7.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "HomeSortView.h"
#import "FLImageView.h"
#import "HomePriorityInfo.h"
#import "WebViewController.h"

@interface HomeSortView()

@property (nonatomic, strong) FLImageView *nineIcon;
@property (nonatomic, strong) UILabel *nineLabel;
@property (nonatomic, strong) UIButton *nickButton;

@end

@implementation HomeSortView

- (void)setModel:(CategortModel *)model {
	_model = model;
	self.nineIcon.url = model.icon;
	[self.nineIcon setUrlNotHaveCircle:model.icon];
	self.nineLabel.text = model.name;
}

- (instancetype)initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		self.userInteractionEnabled = YES;
		[self loadAllSubView];
	}
	return self;
}

- (void)loadAllSubView {
	[self addSubviews:@[self.nineIcon,self.nineLabel,self.nickButton]];
	
	[self.nineIcon mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.mas_top);
		make.centerX.equalTo(self.mas_centerX);
		make.size.equalTo(CGSizeMake([self getIconSize], [self getIconSize]));
	}];
	
	[self.nineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.nineIcon.mas_bottom).offset(kQKFitModuleDecimal(6.0));
		make.centerX.equalTo(self.nineIcon.mas_centerX);
	}];
	
	[self.nickButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.edges.equalTo(self);
	}];
}

- (CGFloat)getIconSize {
	CGFloat width = (ScreenWidth - 2 * kQKFitModuleDecimal(17.0) - 4 * kQKFitModuleDecimal(30.0)) / 5;
	return width;
}

- (void)clickNickButton {
	WebViewController *web = [[WebViewController alloc] init];
	web.URL = self.model.targetUrl;
	[self.currentNavigationController pushViewController:web animated:YES];
}

#pragma mark----- lazy init -----

- (FLImageView *)nineIcon {
	if (!_nineIcon) {
		_nineIcon = [[FLImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, [self getIconSize], [self getIconSize])];
	}
	return _nineIcon;
}

- (UILabel *)nineLabel {
	if (!_nineLabel) {
		_nineLabel = [UILabel labelWithText:@"" font:[UIFont systemFontOfSize:kQKFitFont(12)] textColor:COLOR_WITH_HEX(0x4C4947)];
	}
	return _nineLabel;
}

- (UIButton *)nickButton {
	if (!_nickButton) {
		_nickButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[_nickButton addTarget:self action:@selector(clickNickButton) forControlEvents:UIControlEventTouchUpInside];
	}
	return _nickButton;
}

@end

